package com.heima.article.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.feign.article.IArticleClient;
import com.heima.model.article.dtos.ArticleCommentDto;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleListDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.comment.dtos.CommentConfigDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author 陈辉
 * @data 2023 11:27
 */
@RestController
public class ArticleClient implements IArticleClient {
    @Autowired
    private ApArticleService apArticleService;
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @PostMapping("/api/v1/article/save")
    public ResponseResult saveOrUpdate(@RequestBody ArticleDto articleDto) {
       /*try {
            //人为模拟故障...
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return apArticleService.saveArticle(articleDto);
    }

    @PostMapping("/api/v1/article/updateCommentStatus")
    public ResponseResult updateCommentStatus(CommentConfigDto dto) {
        if(dto.getOperation()==0) {
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .eq(ApArticleConfig::getArticleId, dto.getArticleId())
                    .set(ApArticleConfig::getIsComment, false));
        }else {
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .eq(ApArticleConfig::getArticleId, dto.getArticleId())
                    .set(ApArticleConfig::getIsComment, true));
        }
        apArticleService.update(Wrappers.<ApArticle>lambdaUpdate()
                .eq(ApArticle::getId,dto.getArticleId())
                .set(ApArticle::getComment,0));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @PostMapping("/api/v1/article/findNewsComments")
    public PageResponseResult findNewsComments(ArticleCommentDto dto) {
        return apArticleService.findNewsComments(dto);
    }

    @PostMapping("api/v1/article/findByAuthIdAndDate")
    public ResponseResult findByAuthIdAndDate(@RequestBody ArticleListDto dto){
        return apArticleService.findByAuthIdAndDate(dto);
    }

    @Override
    @GetMapping("/api/v1/article/config/comment")
    public Boolean comment(Long articlId) {
        ApArticleConfig apArticleConfig = apArticleConfigService.getOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articlId));
        return  apArticleConfig.getIsComment();
    }
}
